package com.caimiao.miao.page.master.storage.manual;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.caimiao.miao.bean.TicketOut;
import com.caimiao.miao.interfaces.BasePresenterImpl;
import com.caimiao.miao.library.utils.tip.ToastUtil;
import com.caimiao.miao.page.ticket.TicketActivity;

import java.util.Random;

public class ManualPresenter extends BasePresenterImpl<IManualContact.View, IManualContact.Model> implements IManualContact.Presenter {
    public ManualPresenter(IManualContact.View mView) {
        super(mView);
    }

    @Override
    protected IManualContact.Model getModel() {
        return new ManualModel();
    }

    @Override
    public void start(Context context) {
        mView.setHead(mModel.getHead());
        mView.setName(mModel.getName());
    }

    @Override
    public void confirm(Context context, String count) {
        if (TextUtils.isEmpty(count)) {
            ToastUtil.show("请填写出票数量");
            return;
        }
        TicketOut ticket = new TicketOut();
        ticket.setHeadimgurl(mModel.getHead());
        ticket.setNickname(mModel.getName());
        ticket.setTicket_num(count);
        ticket.setOut_trade_no(getRandomString(12));

        Intent intent = new Intent(context, TicketActivity.class);
        intent.putExtra("DATA", ticket);
        context.startActivity(intent);
    }

    private String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";

    private String getRandomString(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; ++i) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
}
